package whf.framework.tools.generator.cfg;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Properties;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author wanghaifeng
 * @create Oct 9, 2006 9:41:27 PM
 * 
 */
public class Config {
	private String driver;

	private String url;

	private String username;

	private String password;

	private String schema;

	private String tableParser;

	private String javaDir;

	private String jspDir;

	private String rootPackage;

	private String subModule;

	private String tablePrefix;

	private String tables;

	private boolean enableModel = true;

	private boolean enableDao = true;

	private boolean enableDaoImp = true;

	private boolean enableService = true;

	private boolean enableServiceImp = true;

	private boolean enableAction = true;

	private boolean enableHbm = true;

	private boolean enableConfig = true;

	private boolean enableMeta = true;

	private boolean enableJsp = true;

	private boolean enableWebservice = true;

	private String tablesSql;
	
	private String filename;
	
	/**
	 * @return Returns the driver.
	 */
	public String getDriver() {
		return driver;
	}

	/**
	 * @param driver
	 *            The driver to set.
	 */
	public void setDriver(String driver) {
		this.driver = driver;
	}

	/**
	 * @return Returns the javaDir.
	 */
	public String getJavaDir() {
		return javaDir;
	}

	/**
	 * @param javaDir
	 *            The javaDir to set.
	 */
	public void setJavaDir(String javaDir) {
		this.javaDir = javaDir;
	}

	/**
	 * @return Returns the jspDir.
	 */
	public String getJspDir() {
		return jspDir;
	}

	/**
	 * @param jspDir
	 *            The jspDir to set.
	 */
	public void setJspDir(String jspDir) {
		this.jspDir = jspDir;
	}

	/**
	 * @return Returns the password.
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            The password to set.
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return Returns the rootPackage.
	 */
	public String getRootPackage() {
		return rootPackage;
	}

	/**
	 * @param rootPackage
	 *            The rootPackage to set.
	 */
	public void setRootPackage(String rootPackage) {
		this.rootPackage = rootPackage;
	}

	/**
	 * @return Returns the subModule.
	 */
	public String getSubModule() {
		return subModule == null ? "" : subModule;
	}

	/**
	 * @param subModule
	 *            The subModule to set.
	 */
	public void setSubModule(String subModule) {
		this.subModule = subModule;
	}

	/**
	 * @return Returns the tableParser.
	 */
	public String getTableParser() {
		return tableParser;
	}

	/**
	 * @param tableParser
	 *            The tableParser to set.
	 */
	public void setTableParser(String tableParser) {
		this.tableParser = tableParser;
	}

	/**
	 * @return Returns the tablePrefix.
	 */
	public String getTablePrefix() {
		return tablePrefix == null ? "" : tablePrefix;
	}

	/**
	 * @param tablePrefix
	 *            The tablePrefix to set.
	 */
	public void setTablePrefix(String tablePrefix) {
		this.tablePrefix = tablePrefix;
	}

	/**
	 * @return Returns the tables.
	 */
	public String getTables() {
		return tables == null ? "" : tables;
	}

	/**
	 * @param tables
	 *            The tables to set.
	 */
	public void setTables(String tables) {
		this.tables = tables;
	}

	/**
	 * @return Returns the url.
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            The url to set.
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return Returns the username.
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            The username to set.
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return Returns the enableAction.
	 */
	public boolean isEnableAction() {
		return enableAction;
	}

	/**
	 * @param enableAction
	 *            The enableAction to set.
	 */
	public void setEnableAction(boolean enableAction) {
		this.enableAction = enableAction;
	}

	/**
	 * @return Returns the enableConfig.
	 */
	public boolean isEnableConfig() {
		return enableConfig;
	}

	/**
	 * @param enableConfig
	 *            The enableConfig to set.
	 */
	public void setEnableConfig(boolean enableConfig) {
		this.enableConfig = enableConfig;
	}

	/**
	 * @return Returns the enableDao.
	 */
	public boolean isEnableDao() {
		return enableDao;
	}

	/**
	 * @param enableDao
	 *            The enableDao to set.
	 */
	public void setEnableDao(boolean enableDao) {
		this.enableDao = enableDao;
	}

	/**
	 * @return Returns the enableDaoImp.
	 */
	public boolean isEnableDaoImp() {
		return enableDaoImp;
	}

	/**
	 * @param enableDaoImp
	 *            The enableDaoImp to set.
	 */
	public void setEnableDaoImp(boolean enableDaoImp) {
		this.enableDaoImp = enableDaoImp;
	}

	/**
	 * @return Returns the enableHbm.
	 */
	public boolean isEnableHbm() {
		return enableHbm;
	}

	/**
	 * @param enableHbm
	 *            The enableHbm to set.
	 */
	public void setEnableHbm(boolean enableHbm) {
		this.enableHbm = enableHbm;
	}

	/**
	 * @return Returns the enableJsp.
	 */
	public boolean isEnableJsp() {
		return enableJsp;
	}

	/**
	 * @param enableJsp
	 *            The enableJsp to set.
	 */
	public void setEnableJsp(boolean enableJsp) {
		this.enableJsp = enableJsp;
	}

	/**
	 * @return Returns the enableMeta.
	 */
	public boolean isEnableMeta() {
		return enableMeta;
	}

	/**
	 * @param enableMeta
	 *            The enableMeta to set.
	 */
	public void setEnableMeta(boolean enableMeta) {
		this.enableMeta = enableMeta;
	}

	/**
	 * @return Returns the enableModel.
	 */
	public boolean isEnableModel() {
		return enableModel;
	}

	/**
	 * @param enableModel
	 *            The enableModel to set.
	 */
	public void setEnableModel(boolean enableModel) {
		this.enableModel = enableModel;
	}

	/**
	 * @return Returns the enableService.
	 */
	public boolean isEnableService() {
		return enableService;
	}

	/**
	 * @param enableService
	 *            The enableService to set.
	 */
	public void setEnableService(boolean enableService) {
		this.enableService = enableService;
	}

	/**
	 * @return Returns the enableServiceImp.
	 */
	public boolean isEnableServiceImp() {
		return enableServiceImp;
	}

	/**
	 * @param enableServiceImp
	 *            The enableServiceImp to set.
	 */
	public void setEnableServiceImp(boolean enableServiceImp) {
		this.enableServiceImp = enableServiceImp;
	}

	/**
	 * @return Returns the schema.
	 */
	public String getSchema() {
		return schema;
	}

	public String getTablesSql() {
		return tablesSql;
	}

	/**
	 * @param schema
	 *            The schema to set.
	 */
	public void setSchema(String schema) {
		this.schema = schema;
	}

	public void setTablesSql(String tablesSql) {
		this.tablesSql = tablesSql;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	/**
	 * @return Returns the enableWebservice.
	 */
	public boolean isEnableWebservice() {
		return enableWebservice;
	}

	/**
	 * @param enableWebservice
	 *            The enableWebservice to set.
	 */
	public void setEnableWebservice(boolean enableWebservice) {
		this.enableWebservice = enableWebservice;
	}
	
	public void saveToPropertiesFile() throws Exception{
		Properties props = new Properties();
		Field[] fields = Config.class.getDeclaredFields();
		for(int i=0; i< fields.length; i++){
			Object value = fields[i].get(this);
			if(value == null){
				value = "";
			}
			props.put(fields[i].getName(), value.toString());
		}
		props.store(new FileOutputStream(filename), this.subModule);
	}
	
	public void loadFromPropertiesFile() throws Exception{
		Properties props = new Properties();
		props.load(new FileInputStream(filename));
		for(Object key: props.keySet()){
			String k = (String)key;
			String v = props.getProperty(k);
			try{
				Field f = Config.class.getDeclaredField(k);
				String fn = f.getName();
				if(fn.startsWith("enable")){
					f.set(this, new Boolean("true".equalsIgnoreCase(v)));
				} else {
					f.set(this, v);
				}
			} catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception{
		Config c = new Config();
		c.setFilename("e:\\1.properties");
		//c.setDriver("com.mysql.jdbc.Driver");
		c.loadFromPropertiesFile();
		System.out.println(c);
	}

	/**
	 * @return Returns the filename.
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename The filename to set.
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}
}
